//
//  ChangeCityViewController.m
//  Today
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "ChangeCityViewController.h"

@interface ChangeCityViewController ()

@end

@implementation ChangeCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //将全国城市代码封装到数组dataarray(里面是字典(两个key值city，number))
//    NSString *path = @"/Users/dllo/Desktop/MyToday/weather.txt";
//    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSArray *array = [str componentsSeparatedByString:@"|"];
//    NSMutableArray *array1 = [NSMutableArray array];
//    for (NSString *str1 in array) {
//       NSArray *array2 = [str1 componentsSeparatedByString:@":"];
//        [array1 addObject:array2];
//    }
//    self.dataarray = [NSMutableArray array];
//    for (NSArray *data in array1) {
//            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//            [dic setObject:[data objectAtIndex:0]  forKey:@"city"];
//            [dic setObject:[data objectAtIndex:1] forKey:@"number"];
//            [self.dataarray addObject:dic];
//    }
//    NSLog(@"%@", self.dataarray);
    ser = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
    ser.placeholder = @"请输入城市";
    ser.backgroundColor = [UIColor redColor];
    ser.delegate = self;
    ser.barTintColor = [UIColor redColor];
    self.cityarray = [NSMutableArray array];
    self.dataarray = [NSMutableArray array];
    self.numarray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic  in array) {
        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
        [dic1 setObject:[dic objectForKey:@"cityName"]  forKey:@"city"];
        
        [self.cityarray addObject:[dic objectForKey:@"cityName"]];
        [self.numarray addObject:[dic objectForKey:@"cityId"]];
        [dic1 setObject:[dic objectForKey:@"cityId"] forKey:@"number"];
        [self.dataarray addObject:dic1];
    }
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(100, 30);
    layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumLineSpacing = 5;
    collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 , self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    collection.dataSource = self;
    collection.delegate = self;
    collection.backgroundColor = [UIColor blackColor];
    [collection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collection];
    [self.view addSubview:ser];
    // Do any additional setup after loading the view.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataarray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath ];
    NSDictionary *datadic = [self.dataarray objectAtIndex:indexPath.row];
    cell.label.text = [datadic objectForKey:@"city"];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController popViewControllerAnimated:NO];
    NSDictionary *datadic = [self.dataarray objectAtIndex:indexPath.row];
    [self.mydelegate Citynumber:[[datadic objectForKey:@"number"] integerValue]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [ser resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [ser resignFirstResponder];
    NSInteger ij = 0;
    for (int i = 0; i < self.cityarray.count; i++) {
        if ([self.cityarray[i] isEqualToString:ser.text]) {
            [self.mydelegate Citynumber:[self.numarray[i] integerValue]];
            [self.navigationController popViewControllerAnimated:NO];
            ij = 0;
            break;
        } else {
            ij = 1;
        }
    }
    if (ij == 1) {
        SIAlertView *alert = [[SIAlertView alloc] initWithTitle:@"出错了" andMessage:@"没有你要找的城市"];
        [alert show];
        [UIView animateWithDuration:3 animations:^{
            alert.alpha = 0;
        } completion:^(BOOL finished) {
            [alert dismissAnimated:YES];
            [ser becomeFirstResponder];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
